import 'package:flutter/material.dart';

class HouseItem extends StatefulWidget {
  const HouseItem({super.key, required this.itemMap});
  final Map<String, dynamic> itemMap;

  @override
  State<HouseItem> createState() => _HouseItemState();
}

class _HouseItemState extends State<HouseItem> {
  // Helper method to get status text based on status code
  String getStatusText() {
    // Add null check and default value
    final status = widget.itemMap["status"] as int?;
    switch (status) {
      case 1:
        return "正在审核";
      case 2:
        return "审核成功";
      case 3:
        return "审核失败";
      default:
        return "未知"; // Handle unknown or null status
    }
  }

  // Helper method to get status colors based on status code
  List<Color> getStatusColor() {
    // Add null check and default value
    final status = widget.itemMap["status"] as int?;
    switch (status) {
      case 1: // 正在审核 - 蓝色
        return const [
          Color.fromARGB(50, 64, 158, 255), // Background (light blue)
          Color.fromARGB(255, 64, 158, 255) // Text (blue)
        ];
      case 2: // 审核成功 - 绿色
        return const [
          Color.fromARGB(50, 103, 194, 58), // Background (light green)
          Color.fromARGB(255, 103, 194, 58) // Text (green)
        ];
      case 3: // 审核失败 - 红色
        return const [
          Color.fromARGB(50, 245, 108, 108), // Background (light red)
          Color.fromARGB(255, 245, 108, 108) // Text (red)
        ];
      default: // 未知或默认 - 灰色
        return const [
          Color.fromARGB(50, 144, 147, 153), // Background (light grey)
          Color.fromARGB(255, 144, 147, 153) // Text (grey)
        ];
    }
  }

  @override
  Widget build(BuildContext context) {
    // 使用 ?. 和 ?? 操作符提供默认值，防止 null 或 key 不存在导致错误
    final String point = widget.itemMap['point']?.toString() ?? '未知小区';
    final String room = widget.itemMap['room']?.toString() ?? '未知房间';
    final String ownerName = widget.itemMap['name']?.toString() ?? '未知业主';

    // 获取状态颜色
    List<Color> colors = getStatusColor();

    return Container(
      padding: const EdgeInsets.all(10),
      decoration: const BoxDecoration(
        color: Colors.white,
        // 可以考虑给整个 Item 添加圆角
        // borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                  child: Text(point,
                      style: const TextStyle(
                          fontWeight: FontWeight.bold))), // 加粗小区名
              const Spacer(),
              // --- 替换原来的状态显示 ---
              // Container(
              //   padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 5),
              //   decoration: BoxDecoration(
              //     color: const Color.fromARGB(50, 91, 177, 227),
              //     borderRadius: BorderRadius.circular(5),
              //   ),
              //   child: const Text(
              //     '已绑定',
              //     style: TextStyle(
              //       color: Color.fromARGB(255, 85, 145, 175),
              //     ),
              //   ),
              // )
              // --- 使用新的状态显示逻辑 ---
              Container(
                padding: const EdgeInsets.symmetric(
                    vertical: 3, horizontal: 8), // 调整内边距
                decoration: BoxDecoration(
                  color: colors[0], // 使用背景色
                  borderRadius: BorderRadius.circular(5),
                ),
                child: Text(
                  getStatusText(), // 获取状态文本
                  style: TextStyle(
                    color: colors[1], // 使用文本颜色
                    fontSize: 12, // 可以调整字体大小
                  ),
                ),
              )
              // --- 结束替换 ---
            ],
          ),
          const SizedBox(height: 15),
          Row(
            children: [
              const Text(
                '房间号',
                style: TextStyle(color: Colors.grey),
              ),
              const Spacer(),
              Expanded(
                child: Text(
                  room,
                  textAlign: TextAlign.right,
                ),
              ),
            ],
          ),
          const SizedBox(height: 15),
          Row(
            children: [
              const Text(
                '业主',
                style: TextStyle(color: Colors.grey),
              ),
              const Spacer(),
              Expanded(
                child: Text(
                  ownerName,
                  textAlign: TextAlign.right,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
